package leetcode;

import java.net.Inet4Address;

public class Code8 {
    public static void main(String[] args) {
        System.out.println(new Code8().myAtoi("    -43"));
    }
    public int myAtoi(String str) {
        if (str==null ||str.equals(""))
            return 0;
        char [] cs = str.toCharArray();
        char [] c2 = new char[cs.length];
        int end = 0;
        boolean weiBefore = true;
        boolean weiStart = true;
        for (int a=0;a<cs.length;a++){
            if (cs[a]==' '&&weiStart){
                continue;
            }
            else if ((cs[a]=='-'||cs[a]=='+')&&weiBefore){
                weiBefore=false;
                c2[end]=cs[a];
                end++;
                weiStart=false;
            }
            else if (cs[a]>='0'&&cs[a]<='9'){
                c2[end]=cs[a];
                end++;
                weiStart=false;
                weiBefore=false;
            }else{
                break;
            }
        }
        try{
            if (end==0||(end==1&&(c2[0]<'0'||c2[0]>'9'))){
                return 0;
            }
            return Integer.parseInt(new String(c2,0,end));
        }catch (Exception e){
            if (c2[0]=='-'){
                return Integer.MIN_VALUE;
            }else{
                return Integer.MAX_VALUE;
            }
        }
    }
}
